#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "game.h"
#include<QFile>
#include<QKeyEvent>
#include<QMessageBox>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),game(":/maps/level0.txt")
{
    ui->setupUi(this);
    resize(600,600);
    /*g=&game;
    resize(800,600);
    this->setWindowTitle("bomberman");//设置窗口标题
    QPushButton *btn1 = new QPushButton;//按钮对象，new由QObject类自动释放
    btn1->setParent(this);//this为widget类的指针
    btn1->setText("restart");//显示文本
    btn1->resize(200,100);//设置大小
    btn1->move(600,0);//设置按钮位置
    QPushButton *btn2 = new QPushButton;//按钮对象
    btn2->setParent(this);//this为widget类的指针
    btn2->setText("exit");//显示文本
    btn2->resize(200,100);//设置大小
    btn2->move(600,90);
    //信号和槽(链接):1信号发出者(指针）2发送的信号(地址) 3信号接收者(指针) 4处理槽函数(槽函数地址)
    //void(Game::*load)()
    //connect(btn1,&QPushButton::clicked,g,&Game::loadMap);
    connect(btn2,&QPushButton::clicked,this,&MainWindow::close);*/
}

MainWindow::~MainWindow()
{
    delete ui;

}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Up)
    {
        game.moveUp();
    }
    if(event->key() == Qt::Key_Down)
    {
        game.moveDown();
    }
    if(event->key() == Qt::Key_Right)
    {
        game.moveRight();
    }
    if(event->key() == Qt::Key_Left)
    {
        game.moveLeft();
    }
    if(event->key() == Qt::Key_W)
    {
        game.setBomb();
    }
    if(game.isEnd())
        QMessageBox::information(this,"提示","游戏结束");
    this->repaint();
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    game.draw(painter);
}


